package com.bds.mybatis.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bds.mybatis.api.Page;
import com.bds.mybatis.entity.Emp;
import com.bds.mybatis.mapper.EmpMapper;
import freemarker.ext.beans.HashAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author jack
 * @since 2019-07-15
 */
@RestController
@RequestMapping("/sys/emp")
public class EmpController {

    @Autowired
    EmpMapper empMapper;



    /**
     * 通过id查emp
     * @return
     */
    @RequestMapping("/getEmpById")
    public Emp getEmpById(){
        //参数构建
        Serializable empno = 7369;
        //直接调用接口
        Emp emp = empMapper.selectById(empno);
        return emp;
    }

    /**
     * where id in (7369,7499);
     * 查询（根据ID 批量查询）
     * @return
     */
    @RequestMapping("/getEmpListByIds")
    public List<Emp> getEmpListByIds(){
        //构参
        List<Integer> list = new ArrayList();
        list.add(7369);
        list.add(7499);

        //调接
        List<Emp> emps = empMapper.selectBatchIds(list);
        return emps;
    }

    /**
     * 相当与 where empno = 7654 and job = "推销员"
     * @return
     */
    @RequestMapping("/getEmpListByMap")
    public List<Emp> getEmpListByMap(){
        //构参
        Map columnMap = new HashMap();
        columnMap.put("empno","7654");
        columnMap.put("job","推销员");

        //调接
        List<Emp> list = empMapper.selectByMap(columnMap);
        return list;
    }
    /**
     * 查询job为职员的员工信息
     * SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno FROM emp WHERE empno = ? AND job = ?
     * @return
     */
    @RequestMapping("/getEmpList")
    public List<Emp> getEmpList(){
        /**条件构造器*/
        QueryWrapper<Emp> qw = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        map.put("empno","7654");
        map.put("job","推销员");
        qw.allEq(map);
        List<Emp> emps = empMapper.selectList(qw);
        return emps;
    }


    /**
     * 根据条件查询一条结果
     * where comm = 300 此处必须返回一条结构，否则报错500
     *
     */
    @RequestMapping("/selectOne")
    public Emp selectOne(){
        //构参
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        Emp emp = new Emp();
        emp.setComm(300);
        wrapper.setEntity(emp);
        //调接
        Emp emp1 = empMapper.selectOne(wrapper);
        return emp1;
    }


    /**
     * 根据条件查询数量
     * select count(0) from emp where comm is null;
     * @return
     */
    @RequestMapping("/selectCount")
    public int selectCount(){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        Emp emp = new Emp();
        emp.setComm(null);
        wrapper.setEntity(emp);
        Integer integer = empMapper.selectCount(wrapper);
        return integer;
    }

    /**
     * 通过条件构造，查询List<Emp>
     * SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno FROM emp WHERE deptno = ?
     * @return
     */
    @RequestMapping("/selectMaps")
    public List<Map<String, Object>> selectMaps(){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        Map<String,Object> map = new HashMap();
        map.put("deptno",20);
        wrapper.allEq(map);
        List<Map<String, Object>> maps = empMapper.selectMaps(wrapper);
        return maps;
    }

    /**
     * 分页查询
     * @return
     */
    @RequestMapping("/selectPage")
    public List<Emp> selectPage(){
        //构参
        int pageNo = 0;
        int pageSize = 5;
        Map<String,Integer> map = new HashMap<>();
        map.put("pageNo",pageNo);
        map.put("pageSize",pageSize);

        List<Emp> emps = empMapper.queryPage(map);

        return emps;
    }

}
